#!/bin/sh
# inject config data into container

set -e
if [ -f /usr/share/zoneinfo/Asia/Shanghai ];then
  echo "tz set to Asia/Shanghai"
  ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  export TZ="Asia/Shanghai"
else
  echo "warn: no tzdata"
fi

mkdir -p /cfg/
if [ -f /cfg/env.txt ]; then
    echo "/cfg/env.txt mounted"
    set -a # automatically export all variables
    . /cfg/env.txt
    set +a
    echo "import  $(wc -l /cfg/env.txt) env vars from /cfg/env.txt done"
else
    echo "/cfg/env.txt not found!"
fi
if [ -f /docker/initdata/init.sh ]; then
    echo "/docker/initdata/init.sh"
    sh "/docker/initdata/init.sh"
fi
if [ -z "${HOSTNAME}" ];then
    HOSTNAME=no-name-service
fi
# 当前环境变量没有配置相关内容，则启用默认配置
if [ -z "${SKYWALKING_ROOT}" ];then
    SKYWALKING_ROOT="/sk-agent"
fi
if [ -z "${SKYWALKING_HOME}" ];then
    SKYWALKING_HOME="${SKYWALKING_ROOT}/skywalking-agent/"
fi
K8S_NS_FILE="/var/run/secrets/kubernetes.io/serviceaccount/namespace"
if [ -f "${K8S_NS_FILE}" ];then
K8S_NS=`head -n 1 ${K8S_NS_FILE}`
else
K8S_NS="cant-get-ns"
fi
SVC_NAME=`echo ${K8S_NS}.${HOSTNAME} | rev | cut -d'-'  -f 3- | rev`
MY_K8S_NS=${K8S_NS}
MY_K8S_SVC_NAME=$(echo ${SVC_NAME} | awk -F. '{print $NF}')
echo "SVC_NAME=${SVC_NAME}"
echo "MY_K8S_NS=${MY_K8S_NS}"
echo "MY_K8S_SVC_NAME=${MY_K8S_SVC_NAME}"
export MY_K8S_NS
export MY_K8S_SVC_NAME


echo -n '
#!/bin/sh
if [ -f /cfg/env.txt ];then  
    # first run
    if [ ! -f /tmp/env.pre.md5 ];then
        md5sum  /cfg/env.txt > /tmp/env.pre.md5
        exit 0
    else
        md5sum  /cfg/env.txt > /tmp/env.now.md5
        diff /tmp/env.pre.md5 /tmp/env.now.md5
        # cfg change
        ret=$?
        if [ ${ret} -ne 0 ];then
            exit 1
        fi
        /bin/cp -f /tmp/env.now.md5 /tmp/env.pre.md5
     fi
fi
' > /tmp/check_env_change.sh
chmod +x /tmp/check_env_change.sh

if [ ! -z "$(which java)" ];then
    if [ -d '/pp-agent' ];then
    PPAGENT=`find /pp-agent/pinpoint-bootstra* |head -n 1`
    fi
    if [ -n "${PPAGENT}" ] && [ -n "${DISABLE_PINPOINT}" ];then
      PINPOINT_OPTS="-javaagent:${PPAGENT} -Dpinpoint.agentId=${HOSTNAME:0:22} -Dpinpoint.applicationName=${SVC_NAME:0:22}"
      echo "enabled pinpoint apm"
    fi
	  if [ -d "${SKYWALKING_ROOT}" ];then
      SWAGENT=`find "${SKYWALKING_HOME}" -name 'skywalking-agent.jar' |head -n 1`
    fi
    if [ -n "${SWAGENT}" ] && [ -z "${DISABLE_SKYWALKING}" ];then
      # 当环境变量中没有定义地址，则采用默认地址
      if [ -z "${SKYWALKING_ADDRESS}" ];then
        SKYWALKING_ADDRESS="skywalking:11800"
      fi
      SKYWALKING_OPTS="-javaagent:${SWAGENT} -Dskywalking.collector.backend_service=${SKYWALKING_ADDRESS} -Dskywalking.agent.namespace=${MY_K8S_NS} -Dskywalking.agent.service_name=${MY_K8S_SVC_NAME}"
      echo "enabled skywalking apm"
    fi

    # 没有定义SPRING_PROFILES_ACTIVE 采用默认方式
    if [ -z "${SPRING_PROFILES_ACTIVE}" ];then
      SPRING_PROFILES_ACTIVE="dev"
    fi

    JAVA_OPTS="${SKYWALKING_OPTS} -Dspring.profiles.active=${SPRING_PROFILES_ACTIVE}  -server -XX:+UseG1GC -verbose:gc -XX:+PrintCommandLineFlags -XX:+ExplicitGCInvokesConcurrent -Xlog:gc*,safepoint:/data/logs/gc.log:time,uptime:filecount=100,filesize=50M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/data/logs/oom.dump"

    jar=$(find /data/app/ -name "*.jar" 2>/dev/null |head -n 1)
    echo "jar=${jar}"
	  echo "SKYWALKING_OPTS=${SKYWALKING_OPTS}"
    echo "JAVA_OPTS=${JAVA_OPTS}"
    if [ -n "${jar}" ];then
      echo "run ${jar}"
      exec java $JAVA_OPTS  -jar ${jar}
    else
      echo "cant detect /app.jar, will exit"
      sleep 20; 
      exit 1
    fi
fi
